from django.shortcuts import render,HttpResponse,redirect,HttpResponsePermanentRedirect
from rest_framework.views import APIView
from rest_framework.response import Response
from captcha.image import ImageCaptcha
import jwt,redis,random,json,requests
import string,re,time
from zbj.my_redis import r
from zbj.models import *
from zbj.sms_code import sms_code
from zbj.my_jwt import jj
from zbj.urlis.tool import Get_resource,get_role,fenDemand,Hashdelivery
from qiniu import Auth

# rbac权限管理
class ILogin(APIView):
    def post(self,request):
        name=request.data.get("name")
        mobile=request.data.get("mobile")
        if not all([name,mobile]):
            return Response({"code":400,'msg':'字段不完整'})
        try:
            list,urllist=get_role(name,mobile)
            res = I_Login.objects.get(name=name, mobile=mobile)
            token=jj.jwt_encode(
                data={"data": {"id": res.id, "mobile": res.mobile}}
            )
            print("list>>>>",list)
            print("urllist>>>>", urllist)
            return Response({"code":200,'msg':'登陆成功','list':list,'token':token,'userid':res.id,'urllist':urllist})
        except Exception as a:
            return Response({"code":400,'msg':'没有该用户'})
    # 获取所有按钮权限
    def get(self,request):
        buttname=r.get_str("button_list")
        if buttname:
            print("buttname>>>",buttname)
            buttonlist=buttname.decode()
            buttonlist=eval(buttonlist)
            print('>>>>>>>>>>>>>>>>',type(buttonlist))
            return Response({"code":200,'msg':buttonlist})
        else:
            button=Button_name.objects.all()
            buttonlist=[]
            for i in button:
                buttonlist.append({
                    "key":i.id,
                    "label":i.name
                })
                res=r.set_str("button_list",str(buttonlist))
                print("res>>>>>>>>>>>>",res)
            return Response({"code":200,'msg':buttonlist})

# rbac权限配置
class P_rbacbutton(APIView):
    def get(self,request):
        bid=request.query_params.get('bid')
        list=[]
        try:
            res=Resource_button.objects.filter(resource_id=int(bid))
            for i in res:
                list.append(i.id)
            return Response({"code":200,'msg':list})
        except Exception as a:
            return Response({"code":200,'msg':list})
    def post(self,request):
        list1=request.data.get("value")
        bid=request.data.get('bid')
        Resource_button.objects.filter(resource=bid).delete()
        for i in list1:
            Resource_button.objects.create(
                resource_id=bid,
                button_id=i
            )
        return Response({"code":200,'msg':'ok'})

# 显示按钮
class get_bn(APIView):
    def post(self,request):
        url=request.data.get("url")
        print("url>>>>>>>>>",url)
        if not all([url]):
            return Response({"code":400,'msg':'你个2B'})
        res=I_resource.objects.get(url=url)
        res2=Resource_button.objects.filter(resource=res.id)
        list=[]
        for i in res2:
            list.append(i.button_id)
        print('list>>>>>>>>',list)
        return Response({"code":200,'msg':list})

# 获取所有资源
class get_resource(APIView):
    def get(self,request):
        res=I_resource.objects.exclude(uid=0)
        list=[]
        for i in res:
            list.append({
                "id":i.id,
                "name":i.name
            })
        return Response({"code":200,'msg':list})

# 显示管理员审核页面
class S_Demand(APIView):
    def get(self,request):
        userid=request.query_params.get("userid")
        page = request.query_params.get("page")
        list=fenDemand(page,userid)
        return Response({"code":200,'msg':list})
# 需求审核
    def post(self,request):
        code=request.data.get("code")
        state=request.data.get("state")
        if not all([code,state]):
            return Response({"code":400,'msg':'字段不完整'})
        number=Hashdelivery(int(code))
        if number == 0:
            tablename1 = 'Demand'
            tablename = globals().get(tablename1)
            tablename.objects.filter(code=code).update(state=int(state))
            return Response({"code":200,'msg':'审核成功'})
        else:
            tablename1 = f'Demand{number}'
            tablename = globals().get(tablename1)
            tablename.objects.filter(code=code).update(state=int(state))
            return Response({"code": 200, 'msg': '审核成功'})


# 瞎胡写的一个统计图
class Get_tjt(APIView):
    def get(self,request):
        list=[]
        res=Demand.objects.all()
        res2=Demand1.objects.all()
        res3=Demand2.objects.all()
        datalist = res.union(res2).union(res3).all()
        for i in datalist:
            list.append({
                "name":i.project_name,
                "value":float(i.price)
            })
        return Response({"code":200,'msg':list})







